<template>
  <div id="app">
    <el-row>
      <el-col :span="24">
        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <span>航线列表</span>
              <el-button type="primary" @click="handleAddAirline">添加航线</el-button>
            </div>
          </template>
          <el-table :data="airlines" border style="width: 100%">
            <el-table-column prop="airlineNumber" label="航线编号">
            </el-table-column>
            <el-table-column prop="departureCityName" label="起飞城市">
            </el-table-column>
            <el-table-column prop="departureAirportName" label="起飞机场">
            </el-table-column>
            <el-table-column prop="arrivalCityName" label="目的城市">
            </el-table-column>
            <el-table-column prop="arrivalAirportName" label="目的机场">
            </el-table-column>
            <el-table-column prop="distance" label="飞行距离(km)">
            </el-table-column>
<!--            <el-table-column label="操作" width="180">-->
<!--              <template #default="scope">-->
<!--                <el-button type="primary" size="small" @click="handleEditCity(scope.row)">编辑</el-button>-->
<!--                <el-button type="danger" size="small" @click="handleDeleteCity(scope.row)">删除</el-button>-->
<!--              </template>-->
<!--            </el-table-column>-->
          </el-table>
        </el-card>
      </el-col>
    </el-row>

    <el-dialog v-model="dialogVisible" title="添加航线" width="30%">
      <el-form :model="form" label-width="80px">
        <el-form-item label="起飞城市">
          <el-select v-model="selectDepartureProvinceCode" placeholder="请选择省份" @change="handleDepartureProvinceChange">
            <el-option
                v-for="province in provinces"
                :key="province.provinceCode"
                :label="province.provinceName"
                :value="province.provinceCode"
            />
          </el-select>
          <el-select v-model="form.departureCityCode" placeholder="请选择城市">
            <el-option
                v-for="city in selectDepartureProvince.cities"
                :key="city.cityCode"
                :label="city.cityName"
                :value="city.cityCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="目的城市">
          <el-select v-model="selectArrivalProvinceCode" placeholder="请选择省份" @change="handleArrivalProvinceChange">
            <el-option
                v-for="province in provinces"
                :key="province.provinceCode"
                :label="province.provinceName"
                :value="province.provinceCode"
            />
          </el-select>
          <el-select v-model="form.arrivalCityCode" placeholder="请选择城市">
            <el-option
                v-for="city in selectArrivalProvince.cities"
                :key="city.cityCode"
                :label="city.cityName"
                :value="city.cityCode"
            />
          </el-select>
          <el-form-item label="起飞机场">
            <el-input v-model="form.departureAirportName" />
          </el-form-item>
          <el-form-item label="目的机场">
            <el-input v-model="form.arrivalAirportName" />
          </el-form-item>
          <el-form-item label="飞行距离">
            <el-input v-model="form.distance" />
          </el-form-item>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSaveAirline">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios';
import { ElMessage } from 'element-plus';

export default {
  name: 'AdminAirline',
  data() {
    return {
      cities: [],
      provinces: [],
      airlines: [],
      selectDepartureProvinceCode: null,
      selectDepartureProvince:{},
      selectArrivalProvinceCode: null,
      selectArrivalProvince:{},
      dialogVisible: false,
      form: {
        departureCityCode: '',
        arrivalCityCode: '',
        distance: '',
        departureAirportName: '',
        arrivalAirportName: '',
      },
    };
  },
  mounted() {
    this.fetchCities();
    this.fetchProvinces();
    this.fetchAirlines();
  },
  methods: {
    handleDepartureProvinceChange(){
      this.selectDepartureProvince = this.provinces.find(province => province.provinceCode === this.selectDepartureProvinceCode);
    },
    handleArrivalProvinceChange(){
      this.selectArrivalProvince = this.provinces.find(province => province.provinceCode === this.selectArrivalProvinceCode);
    },
    fetchAirlines() {
      axios.get('/airline')
          .then(response => {
            this.airlines = response.data;
          })
          .catch(error => {
            console.error('获取航空公司列表失败', error);
            ElMessage.error('获取航空公司列表失败');
          });
    },
    fetchCities() {
      axios.get('/cities')
          .then(response => {
            this.cities = response.data;
          })
          .catch(error => {
            console.error('获取城市列表失败', error);
            ElMessage.error('获取城市列表失败');
          });
    },
    fetchProvinces() {
      axios.get('/provinces')
          .then(response => {
            this.provinces = response.data;
          })
          .catch(error => {
            console.error('获取省份列表失败', error);
            ElMessage.error('获取省份列表失败');
          });
    },
    handleAddAirline() {
      this.dialogVisible = true;
      this.form= {
        departureCityCode: '',
        arrivalCityCode: '',
        distance: '',
        departureAirportName: '',
        arrivalAirportName: '',
      };
    },
    handleSaveAirline() {
      const AirlineDto = {
        departureCityCode: this.form.departureCityCode,
        arrivalCityCode: this.form.arrivalCityCode,
        distance: this.form.distance,
        departureAirportName: this.form.departureAirportName,
        arrivalAirportName: this.form.arrivalAirportName,
      };
      axios.post('/airline', AirlineDto)
          .then(() => {
            this.dialogVisible = false;
            this.fetchAirlines();
            ElMessage.success('添加航线成功');
          })
          .catch(error => {
            console.error('添加航线失败', error);
            ElMessage.error('添加航线失败');
          });
    },
    // handleEditCity(city) {
    //   // TODO: 实现编辑航线功能
    //   console.log('编辑航线', city);
    // },
    // handleDeleteCity(city) {
    //   // TODO: 实现删除航线功能
    //   console.log('删除航线', city);
    // },
  },
};
</script>

<style scoped>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

.box-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>